global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Figure_A2_autotrends.log, replace name(fig)

capture noi {



    ************************************************************
    * Trends in automation (for biadic applications)
    ************************************************************

    clear

    * Set global color scheme.
    qui include ${code_dir}/config/figuretools.do
    global color cl
    if "${color}" == "cl" {
        colorpalette scico bamO, n(6) saturate(40) intensity(0.9) nograph  // intensity(0.93) saturate(10) //  // sico Bold; scico roma
        local cpl_name "_cl"
        local color1 "`r(p1)'"
        local color2 "`r(p2)'"
        local color3 "`r(p3)'"
        local color4 "`r(p4)'"
        local color5 "`r(p5)'"
        local color6 "`r(p6)'"
    }

    * Prepare data
    use ${final_dir}/family_timeseries_full.dta, clear
    keep if biadic
    keep if year >= 1980 & year <= 2015
    sort year docdb_family_id
    by year: gen total_pate_peryear = _N
    by year (docdb_family_id): egen total_patent_auto90 = sum(auto90)
    by year (docdb_family_id): egen total_patent_auto95 = sum(auto95)
    keep year total_pate_peryear total_patent_auto90 total_patent_auto95 
    duplicates drop
    gen share_auto90 = total_patent_auto90 / total_pate_peryear
    gen share_auto95 = total_patent_auto95 / total_pate_peryear
    gen pct_auto90 = total_patent_auto90 / total_pate_peryear * 100
    gen pct_auto95 = total_patent_auto95 / total_pate_peryear * 100
    replace total_patent_auto95 = total_patent_auto95/1000
    replace total_patent_auto90 = total_patent_auto90/1000

    * Output figures

    * figure a : share

    twoway connected pct_auto90 pct_auto95 year, ///
        xlabel(1980 (5) 2015, nogrid) ylabel(0 (1) 5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) yscale(r(0 5)) ///
        lwidth(1.3pt 1.3pt) lpat(dash dash) msym(O D) msize(small small)  ///
        ytitle("Automation (%)") xtitle("Year") ///
        text(4.05 2009.8 "Auto90", color(`crm6')) text(2.6 2008.2 "Auto95", color(`crm3')) ///
        legend(off) yscale(titlegap(*-4)) ///
        plotregion(margin(b=0)) ///
        title("(A)  Share automation out of all patents", position(6)) /// Automation in machinery patenting worldwide
        color("`crm6'" "`crm3'") ///
        name(sh_auto90_auto95_all, replace)


    * figure b : log total

    twoway connected total_patent_auto90 total_patent_auto95 year, ///
        xlabel(1980 (5) 2015, nogrid) ylabel(#5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) yscale(log) ///
        lwidth(1.3pt 1.3pt) lpat(dash dash) msym(O D) msize(small small)  ///
        ytitle("Log automation patents") xtitle("Year") ///
        text(12 2009.8 "Auto90", color(`crm6')) text(6.6 2008.2 "Auto95", color(`crm3')) ///
        legend(off) yscale(titlegap(*-4)) ///
        plotregion(margin(b=0)) ///
        title("(B)  Number of automation patents", position(6)) ///
        color("`crm6'" "`crm3'") ///
        name(nr_auto90_auto95, replace)

    * Combine
    graph combine sh_auto90_auto95_all nr_auto90_auto95, xsize(12) ysize(7)
    grc1leg2 sh_auto90_auto95_all nr_auto90_auto95, xsize(12) ysize(7) loff 
    graph export ${fig_dir}/appendix/Figure_A2_autotrends.pdf, as(pdf) replace
    graph export ${fig_dir}/appendix/Figure_A2_autotrends.eps, as(eps) replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig